package com.blockmeta.bbs.businesslibrary.widget.chartview.Axis;


import com.blockmeta.bbs.baselibrary.utils.FormatUtil;
import com.blockmeta.bbs.baselibrary.utils.TimeUtil;
import com.blockmeta.bbs.businesslibrary.websocket.Market;
import com.blockmeta.bbs.businesslibrary.websocket.Symbol;
import com.blockmeta.bbs.businesslibrary.widget.chartview.CandleData;
import com.blockmeta.bbs.businesslibrary.widget.chartview.ChartData;
import com.blockmeta.bbs.businesslibrary.widget.chartview.Layer.Layer;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class DailyCoordinate extends Coordinate
{
	private List<Integer> m_listSession = new ArrayList<Integer>();
	
	@Override
	public void setData(ChartData pData)
	{
		
		List<Point> tXList = new ArrayList<Point>();
		List<Point> tYList = new ArrayList<Point>();

		if (pData != null)
		{
			tSymBol = pData.getSymbol();
			ChartData.ChartPeroid mChartType = pData.getChartPeroid();
			Layer.ChartTypeEnum tLayerType = m_callback.getCharTypeEnum();
			int tYTagCount = m_callback.getYTagCount();
			Market.AppMarket appMarket = pData.getSymbol().getMarket().getAppMarket();

			List<Map<String, Integer>> tSession = pData.getSessionSection();
			List<CandleData> tList = pData.getCandleData();
			m_listSession = pData.getSession();
			boolean needYesterMA = (m_callback != null) && m_callback.isShowYesterClose();
			float tYesterMA = m_callback.getYesterClose();
			Symbol tSymBol = pData.getSymbol();
			int tSessionSize = tSession.size();

			int tSubLength = 0;
			//根据服务器获取的时间 count open close
			for (int ii = 0; ii < tSessionSize; ii++)
			{


					if (ii == 0)
					{
						String tXText = String.format("%02d", TimeUtil.getHour(tSession.get(ii).get(ChartData.OPENSTR))) + ":" + String.format("%02d", TimeUtil.getMinute(tSession.get(ii).get(ChartData.OPENSTR)));
						tXList.add(new Point(0, 0, 0, 0, true, tXText, tSubLength + 1));
					}


					tSubLength += tSession.get(ii).get(ChartData.COUNTSTR);
					String tXText = String.format("%02d", TimeUtil.getHour(tSession.get(ii).get(ChartData.CLOSESTR))) + ":" + String.format("%02d", TimeUtil.getMinute(tSession.get(ii).get(ChartData.CLOSESTR)));
					tXList.add(new Point(0, 0, 0, 0, true, tXText, tSubLength + 1));

			}
			
			m_nQueryCount = m_listSession.size();

			float tMaxValue = 0.f;
			float tMinValue = 0.f;
			int tListSize = tList.size();

			float tClose = 0.f;
			for (int ii = 0; ii < tListSize; ii++)
			{
				CandleData tData = tList.get(ii);

				if (tLayerType == Layer.ChartTypeEnum.DAILYBAR)
				{
					tClose = (float) tData.m_lVolume;
				}else if(tLayerType == Layer.ChartTypeEnum.DAILY)
				{
					tClose = (float)tData.getDbClose();
				}

				
				if (ii == 0)
				{
					tMaxValue = tClose;
					tMinValue = tClose;
				}
				else
				{
					if (tClose > tMaxValue)
					{
						tMaxValue = tClose;
					}
					if (tClose < tMinValue)
					{
						tMinValue = tClose;
					}
				}
			}

			if (tMaxValue == 0.f && tMinValue == 0.f)
			{

			}
			else
			{
				if (needYesterMA && tLayerType != Layer.ChartTypeEnum.DAILYBAR)
				{

					if (tYesterMA > tMaxValue)
					{
						tMaxValue = tYesterMA;
					}
					else if (tYesterMA < tMinValue)
					{
						tMinValue = tYesterMA;
					}
				}
			}


			m_bDigit = 2;
			if (tSymBol != null)
			{
				m_bDigit = tSymBol.getDigit();
			}
			if(tLayerType == Layer.ChartTypeEnum.DAILY )
			{
				if (tMaxValue == 0.f && tMinValue == 0.f)
				{
					float tDiff = 2.f / (float) Math.pow(10, 2);

						tMaxValue = tYesterMA + tDiff;
						tMinValue = tYesterMA - tDiff;

				}

				if(tMaxValue == tMinValue)
				{
					float tDiff = 2.f / (float) Math.pow(10, 2);
					tMaxValue = tMinValue + tDiff;
				}
			}
			else if(tLayerType == Layer.ChartTypeEnum.DAILYBAR)
			{
				if (tMaxValue == 0.f && tMinValue == 0.f)
				{
					float tDiff = 2.f;
					tMaxValue = tDiff;
					tMinValue = 0;
				}

				if(tMaxValue == tMinValue)
				{
					float tDiff = 2.f;
					tMinValue = tMaxValue / 2;
				}
			}


			if(tLayerType == Layer.ChartTypeEnum.DAILY )
			{
				if (1 == 1)//以最高最低价，画线时上下要留空隙
				{
					m_nYIntervalCount = 3;
					if(tYTagCount != 0)
					{
						m_nYIntervalCount = tYTagCount;
					}

					float tValueDist = tMaxValue - tMinValue;
					m_nYIntervalStandard = tValueDist / m_nYIntervalCount;

					for (int i = 0; i <= m_nYIntervalCount; i++)
					{
						String tCurrentValue = FormatUtil.getFormatString(tMinValue + m_nYIntervalStandard * i, m_bDigit);
						tYList.add(new Point(0, 0, 0, 0, true, tCurrentValue, (i + 1)));
					}

					m_fMaxValue = tMaxValue;
					m_fMinValue = tMinValue;

				}
				else //需要以昨收价为中线 a股或期货
				{
					float tYesterClose = tYesterMA;
					boolean updist = Math.abs(tMaxValue - tYesterClose) > Math.abs(tMinValue - tYesterClose) ? true : false;
					if (updist)
					{
						tMinValue = tYesterClose - Math.abs(tMaxValue - tYesterClose);
					}
					else
					{
						tMaxValue = tYesterClose + Math.abs(tMinValue - tYesterClose);
					}

					m_nYIntervalCount = 4;
					if(tYTagCount != 0)
					{
						m_nYIntervalCount = tYTagCount;
					}

					float tValueDist = tMaxValue - tMinValue;
					m_nYIntervalStandard = tValueDist / m_nYIntervalCount;

					for (int i = 0; i <= m_nYIntervalCount; i++)
					{
						String tCurrentValue = FormatUtil.getFormatString(tMinValue + m_nYIntervalStandard * i, m_bDigit);
						tYList.add(new Point(0, 0, 0, 0, true, tCurrentValue, (i + 1)));
					}

					m_fMaxValue = tMaxValue;
					m_fMinValue = tMinValue;
				}
			}
			else if(tLayerType == Layer.ChartTypeEnum.DAILYBAR)
			{
				m_nYIntervalCount = 2;
				tMinValue = 0;

				float tValueDist = tMaxValue - tMinValue;
				m_nYIntervalStandard = tValueDist / m_nYIntervalCount;

				String symbolUnit = "";

				String max = FormatUtil.longToVolumeHumanReadableFormat((long)tMaxValue, 2);
				tYList.add(new Point(0, 0, 0, 0, true, symbolUnit, 1));
				tYList.add(new Point(0, 0, 0, 0, true, "", 2));
				tYList.add(new Point(0, 0, 0, 0, true, max,3));

				m_fMaxValue = tMaxValue;
				m_fMinValue = 0;
			}


		}
		else
		{
			
		}

		m_xAxis.setPointList(tXList);
		m_yAxis.setPointList(tYList);
	}
	
	public List<Integer> getSession()
	{
		return m_listSession;
	}
	
	public void setStartIndex(int pIndex)
	{
		m_nStartIndex = pIndex;
	}
}
